package main

import (
	"fmt"
	"os"
)

type Page struct {
	Title string
	Body  []byte
}

var prefix = "E:\\go\\lengxf-go\\6_gowiki\\"
var pageName = "TestPage"

func main() {
	p1 := &Page{Title: pageName, Body: []byte("this is a test page")}
	err := p1.save()
	if err != nil {
		return
	}
	p2 := LoadPage(pageName)
	fmt.Println(string(p2.Body))
}

func (p *Page) save() error {
	fileName := prefix + p.Title + ".txt"
	return os.WriteFile(fileName, p.Body, 0600)
}

func LoadPage(title string) *Page {
	fileName := prefix + title + ".txt"
	body, _ := os.ReadFile(fileName)
	return &Page{
		Title: title,
		Body:  body,
	}
}

func loadPageAndErr(title string) (*Page, error) {
	fileName := prefix + title + ".txt"
	body, err := os.ReadFile(fileName)
	if err != nil {
		return nil, err
	}
	return &Page{
		Title: title,
		Body:  body,
	}, nil
}
